package com.bfxy.rabbitmq.confim;

import com.bfxy.rabbitmq.util.RabbitUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

/**
 * Created by deng_wb on 2020/3/10
 */
public class Producer {

    public static void main(String[] args) throws Exception{
        Channel channel = RabbitUtil.producerChannel();

        // 指定消息投递模式: 消息的确认模式
        channel.confirmSelect();

        String exchangeName = "test_confirm_exchange";
        String routingKey = "confirm.save";

        String msg = "Hello RabbitMQ Send confirm message!";
        channel.basicPublish(exchangeName,routingKey,null,msg.getBytes());

        // 添加一个确认监听器
        channel.addConfirmListener(new ConfirmListener() {

            /**
             *
             * @param deliveryTag   消息的唯一标签
             * @param multiple
             * @throws IOException
             */
            @Override
            public void handleAck(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("---- ack! ----");

            }

            @Override
            public void handleNack(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("---- no ack! ----");
            }
        });

    }

}
